home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_shifteddrtile.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.9 KB  |  103 lines

  1. /*
  2.  * TLShiftedD_RTile.sl -- generates a surface of alternating disks and rings
  3.  *
  4.  * DESCRIPTION:
  5.  *    Will generate alternating and shifted rows of disks and rings
  6.  * 
  7.  *
  8.  * PARAMETERS:
  9.  *    Ka, Kd, Ks - the usual
  10.  *  roughness -  Contols the specular reflection
  11.  *    fuzz -       Amount to blur edge
  12.  *    innerRadius - inner ring
  13.  *    outerRadius - outer ring
  14.  *    sfreq - # of tiles in s
  15.  *    tfreq - # of tiles in t 
  16.  *    cstate1 - foreground color
  17.  *    specularcolor - color of specular highlight
  18.  * 
  19.  * HINTS:
  20.  *    The center varible should really be placed in the parameter list 
  21.  *      (remember to set the right space)
  22.  *  Also, it would be better to set the width of the ring rather than
  23.  *      specifing the inner and outer ring.
  24.  *  Another thing that might be useful is to add a parameter to 
  25.  *      choose which row to do first.
  26.  * 
  27.  * AUTHOR: Tal Lancaster
  28.  *    tal@SpamSucks_cs.caltech.edu
  29.  *
  30.  * History:
  31.  *    Created: 8/12/95
  32.  */ 
  33. #define smoothPulse(a, b, fuzz, loc) \
  34.     (smoothstep (a-fuzz, a+fuzz, loc) - \
  35.     smoothstep (b-fuzz, b+fuzz, loc) )
  36.  
  37. #define repeat(pos, freq) \
  38.     (mod (pos * freq, 1) )
  39.  
  40. #define whichtile(pos, freq) \
  41.     (floor((pos) * freq) )
  42.  
  43. #define isOdd(x) \
  44.     ((mod(x,2) == 1) ? 1 : 0)
  45.  
  46. surface
  47. k3d_shifteddrtile (
  48.     uniform float Ka = 1;
  49.     uniform float Kd = .5;
  50.     uniform float Ks = .5;
  51.     uniform float roughness = .1;
  52.     uniform float fuzz = .025;          /* amount to blur edge */
  53.     uniform float innerRadius = 0.3;    /* inner ring */
  54.     uniform float outerRadius = 0.45;   /* outer ring */
  55.     uniform float sfreq = 4.0;          /* # of tiles in s */
  56.     uniform float tfreq = 4.0;          /* # of tiles in t */
  57.     uniform color cstate1 = color(1, 0, 0);  /* foreground color */
  58.     uniform color specularcolor = 1;)
  59. {
  60.     point Nf;
  61.     uniform point center;      /* Center of disk */
  62.     color surfColor;   /* Color of surface */ 
  63.     float mix_opacity; /* How much to mix between the surfaces */
  64.     float ss, tt;      /* tiled s, t */
  65.     float row, col;    /* used to determine which tile we are in */
  66.     float d;           /* distance from center of current tile */
  67.     float wasOdd;      /* True if test was odd */
  68.     
  69.     Nf = faceforward (normalize(N),I);
  70.  
  71.     surfColor = Cs;
  72.     center = (0.5, 0.5, 0);  /* This should really be */
  73.                              /* put in the paramter list */
  74.  
  75.      row = whichtile (t, tfreq);
  76.     
  77.     if (isOdd(row) == 0) {
  78.         ss = mod (s * sfreq + 0.5, 1);
  79.         wasOdd = 0;
  80.     }
  81.     else {
  82.         ss = repeat (s, sfreq);
  83.         wasOdd = 1;
  84.     }
  85.     
  86.     tt = repeat (t, tfreq);
  87.     d = distance (center, (ss, tt, 0));
  88.  
  89.     if (wasOdd == 1)
  90.         /* Do ring */
  91.         mix_opacity = smoothPulse (innerRadius, outerRadius, fuzz, d);
  92.     else
  93.         /* Do disk */
  94.         mix_opacity = 1 - smoothstep (outerRadius-fuzz, outerRadius+fuzz, d);
  95.         
  96.     surfColor = mix (surfColor, cstate1, mix_opacity);
  97.     
  98.     
  99.     Oi = Os;
  100.     Ci = Os * (surfColor * (Ka*ambient() + Kd*diffuse(Nf)) +
  101.           specularcolor * Ks*specular(Nf,-normalize(I),roughness));
  102. }
  103.